// Package webmanage @author <chengjiang@buffalo-robot.com>
// @date 2023/2/21
// @note
package webmanage

import (
	"context"
	"github.com/go-kratos/kratos/v2/log"
	"gorm.io/gorm"
	"phm/internal/biz"
	"phm/internal/biz/webmanage"
	"phm/models/generator/model"
	"phm/models/generator/query"
)

type sysOperationRepo struct {
	Db  biz.DataRepo
	log *log.Helper
}

func NewSysOperationRepo(data biz.DataRepo, logger log.Logger) webmanage.SysOperationRepo {
	return &sysOperationRepo{Db: data, log: log.NewHelper(logger)}
}

func (u *sysOperationRepo) CreateSysOperation(ctx context.Context, uModel *model.SysOperation, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	opt := query.Use(s).SysOperation
	err := opt.WithContext(ctx).Create(uModel)
	if err != nil {
		return err
	}
	return nil
}

func (u *sysOperationRepo) UpdateSysOperation(ctx context.Context, uModel *model.SysOperation, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	opt := query.Use(s).SysOperation
	info, err := opt.WithContext(ctx).Where(opt.ID.Eq(uModel.ID)).Updates(uModel)
	u.log.Info(info)
	if err != nil {
		u.log.Errorf("更新错误：%v", err)
		return err
	}
	return nil
}

func (u *sysOperationRepo) DeleteSysOperationByIds(ctx context.Context, ids []string, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	role := query.Use(s).SysOperation
	info, err := role.WithContext(ctx).Where(role.ID.In(ids...)).Delete()
	u.log.Info(info)
	if err != nil {
		u.log.Errorf("删除错误：%v", ids)
		return err
	}
	return nil
}

func (u *sysOperationRepo) GetSysOperationById(ctx context.Context, id string) (*model.SysOperation, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).SysOperation
	user, err := opt.WithContext(ctx).Where(opt.ID.Eq(id)).First()
	if err != nil {
		return nil, err
	}
	return user, nil
}

func (u *sysOperationRepo) GetSysOperationByIds(ctx context.Context, ids []string) ([]*model.SysOperation, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).SysOperation
	users, err := opt.WithContext(ctx).Where(opt.ID.In(ids...)).Find()
	if err != nil {
		return nil, err
	}
	return users, nil
}

func (u *sysOperationRepo) GetSysOperationByRoleId(ctx context.Context, roleId string) ([]*model.SysOperation, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).SysOperationRole
	operationRoles, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq(0), opt.RoleID.Eq(roleId)).Find()
	if err != nil {
		return nil, err
	}
	var ids = []string{}
	for _, v := range operationRoles {
		ids = append(ids, *v.OperationID)
	}
	sysOperation := query.Use(s).SysOperation
	if len(ids) < 1 {
		operations, err := sysOperation.WithContext(ctx).Where(sysOperation.IsSoftDelete.Eq(0)).Find()
		if err != nil {
			return nil, err
		}
		return operations, nil
	} else {
		operations, err := sysOperation.WithContext(ctx).Where(sysOperation.IsSoftDelete.Eq(0), sysOperation.ID.In(ids...)).Find()
		if err != nil {
			return nil, err
		}
		return operations, nil
	}
}

func (u *sysOperationRepo) GetDB() *gorm.DB {
	return u.Db.GetDB()
}
